<?php

namespace sakuTec\colorful\pluckSword;

use pocketmine\Player;
use pocketmine\plugin\MethodEventExecutor;
use pocketmine\event\EventPriority;
use pocketmine\item\Item;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;


use sakuTec\colorful\pluckSword\pluckIO;
use sakuTec\colorful\pluckSword\SwordHandler;
use sakuTec\colorful\pluckSword\swords\SwordManager;
use sakuTec\colorful\pluckSword\commands\MainCommand;
use sakuTec\colorful\pluckSword\SwordPath;

class MainThread extends \pocketmine\plugin\PluginBase{

 const ITEM_ID = Item::WOODEN_SWORD;

 public static $instance = null;

 public $data = array();

 public $swords = array();

 public $mainfest = array();

 public $io = null;

 public $listener = null;

 public function onLoad()
{
 self::$instance = $this;
}

 public static function getInstance()
{
 return self::$instance;
}

 public function onEnable()
{
 @mkdir($this->getDataFolder());
 $this->io = new pluckIO($this);
 $this->data = $this->io->readPlayerData();
 $this->listener = new SwordHandler($this);
 $this->registerAllEvents($this->listener);
 $this->cmder = new MainCommand($this);
 $this->getLogger()->info('拔刀Beta');
}


 public function registerAllEvents($ler)
{
 $this->getServer()->getPluginManager()->registerEvent("pocketmine\\event\\player\\PlayerItemHeldEvent", $ler, EventPriority::HIGHEST, new MethodEventExecutor("onItemHeldEvent"), $this);
 $this->getServer()->getPluginManager()->registerEvent("pocketmine\\event\\entity\EntityDamageEvent", $ler, EventPriority::HIGHEST, new MethodEventExecutor("onEntityDamageEvent"), $this);
 $this->getServer()->getPluginManager()->registerEvent("pocketmine\\event\\player\\PlayerJoinEvent", $ler, EventPriority::HIGHEST, new MethodEventExecutor("onPlayerJoinEvent"), $this);
 $this->getServer()->getPluginManager()->registerEvent("pocketmine\\event\\player\\PlayerQuitEvent", $ler, EventPriority::HIGHEST, new MethodEventExecutor("onPlayerQuitEvent"), $this);
}

 public function onDisable()
{
 $this->io->savePlayerData($this->data);
}

 public function setData(Player $player, $type = 3, $level = 1){
 $id = strtolower($player->getName());
 $this->data[$id][$type] = true;
 $this->initSword($player, $type);
}


 public function loadPlayerData(Player $player)
{
 $id = strtolower($player->getName());
 foreach($this->getPlayerData($id) as $key => $v)
  {
   $this->initSword($player,$key);
  }
}


 public function hasPlayerData($pl)
{
  if($pl instanceof Player){
  $id = strtolower($pl->getName());
 }else{
  $id = strtolower($pl);
 }
 return isset($this->data[$id]);
}

public function hasData($pl, $type){
 if($pl instanceof Player){
  $id = strtolower($pl->getName());
 }else{
  $id = strtolower($pl);
 }
 return isset($this->data[$id][$type]);
}


 public function getMainItem()
{
 return self::ITEM_ID;
}

 public function getPlayerData($name)
{
 $id = strtolower($name);
 return isset($this->data[$id]) ? $this->data[$id] : array();
}

 public function removeData($pl, $type)
{
 if($pl instanceof Player){
  $id = strtolower($pl->getName());
 }else{
  $id = strtolower($pl);
 }
 if($this->hasData($pl,$type)){
 unset($this->data[$id][$type]);
 if($type !== 3) $this->removeSword($pl,$type);
 }
}


 public function initSword(Player $player, $type = 3, $level = 1)
{
 $id = strtolower($player->getName());
 $this->swords[$id][$type] = SwordManager::getSword($player, $type, $level);
 //$player->sendMessage('你获得了 >' . $this->swords[$id][$type]->getName());
 $this->setMainSword($player,$type);
 return $this->swords[$id][$type];
}


 public function getPath($n)
{
 return SwordPath::get($n);
}

 public function getString($n)
{
 return SwordPath::getString($n);
}

 public function getMainSword(Player $player)
{
 $id = strtolower($player->getName());
 return isset($this->mainfest[$id]) ? $this->mainfest[$id] : $this->initSword($player);
}

 public function setMainSword(Player $player, $type = 3){
 $id = strtolower($player->getName());
 $this->mainfest[$id] = $this->swords[$id][$type];
}

 public function removeSword(Player $pl, $type)
{
 $id = strtolower($pl->getName());
 if($this->hasSword($id,$type)){
 unset($this->swords[$id][$type]);
 if(isset($this->mainfest[$id])) $this->setMainSword($pl,3);
 }
}


 public function hasSword($pl, $type)
{
 if($pl instanceof Player){
  $id = strtolower($pl->getName());
 }else{
  $id = strtolower($pl);
 }
 return isset($this->swords[$id][$type]);
}


 public function onCommand(CommandSender $sender, Command $cmd, $label, array $ar)
{
 $this->cmder->onCommand($sender,$cmd->getName(),$ar);
}
}